#include <stdio.h>
#include <stdlib.h>
#include "panel.h"

// Init Panel
void initPanel(Panel *panel, int width, int height) {
    panel->width = width;
    panel->height = height;
    
	panel->canvas = (char **)malloc(height * sizeof(char *));
    for (int y = 0; y < height; y++) {
        panel->canvas[y] = (char *)malloc(width * sizeof(char));
        for (int x = 0; x < width; x++) {
            
			// Draw board borders
            if (y == 0 || y == height - 1) {
                panel->canvas[y][x] = '-';
            } else if (x == 0 || x == width - 1) {
                panel->canvas[y][x] = '|';
            } else {
                panel->canvas[y][x] = ' ';
            }
        }
    }

    // Draw the four corners
    panel->canvas[0][0] = '+';
    panel->canvas[0][width - 1] = '+';
    panel->canvas[height - 1][0] = '+';
    panel->canvas[height - 1][width - 1] = '+';
}

// Free Panel
void freePanel(Panel *panel) {
    for (int y = 0; y < panel->height; y++) {
        free(panel->canvas[y]);
    }
    free(panel->canvas);
}

// Print Panel
void printPanel(Panel *panel) {
    for (int y = 0; y < panel->height; y++) {
        for (int x = 0; x < panel->width; x++) {
            printf("%c", panel->canvas[y][x]);
        }
        printf("\n");
    }
}
